﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 会员业务逻辑层
    /// </summary>
    public class MemberManage : MarshalByRefObject
    {
        private static readonly UserDal Dal = new UserDal();

        #region 测试用户是否已经被占用

        /// <summary>
        /// 测试用户是否已经被占用
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool CheckUserName(string userName)
        {
            return Dal.GetInfo(userName) != null;
        }

        #endregion

        /// <summary>
        /// 加载密码问题
        /// </summary>
        /// <param name="name"></param>
        public string LoadQuestion(string name)
        {
            User u = Dal.GetInfo(name);
            if (u == null)
            {
                return "";
            }
            return u.Question;
        }

        /// <summary>
        /// 测试用户名和电邮是否匹配，取回密码第二步
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="eMail"></param> 
        public bool TestEmail(string userName, string eMail)
        {
            User u = Dal.GetInfo(userName);

            if (u.EMail == eMail)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 测试用户名和密码问题，密码答案是否匹配，取回密码第二步
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="question"></param>
        /// <param name="answer"></param>
        public bool TestQA(string userName, string question, string answer)
        {
            User u = Dal.GetInfo(userName);

            if (u.Question == question && u.Answer == answer)
            {
                return false;
            }
            return true;
        }

        #region 修改密码

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="psw1"></param>
        /// <param name="psw2"></param>
        /// <remarks>取回密码用的</remarks>
        public bool PasswordModify(string userName, string psw1, string psw2)
        {
            if (psw1 == psw2)
            {
                psw1 = Hash.Md5(psw1);
                Dal.ChangPSW(userName, psw1);

                return true;
            }
            return false;
        }

        #endregion

        /// <summary>
        /// 搜索会员
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IList<string> SearchMember(string name)
        {
            return Dal.SearchMember(name);
        }

        /// <summary>
        /// 扣点数
        /// </summary>
        /// <param name="pointNum"></param>
        /// <param name="memberId"></param>
        public void KouDianShu(int pointNum, int memberId)
        {
            Dal.KouDianShu(pointNum, memberId);
        }

        #region 显示会员登录界面

        /// <summary>
        /// 显示会员登录界面
        /// </summary>
        public dynamic ShowUserLogin()
        {
            Result res = new Result();

            if (RightsCheck.IsLogined == false)
            {
                var result = new
                {
                    Logined = false,
                    Name = "",
                    PetName = "",
                    Coin = 0,
                    Point = 0,
                    ActiveType = 0
                };
                return result;
            }
            User model = Dal.GetInfo(UserManage.Current.UserID);

            if (model == null)
            {
                var result = new
                {
                    Logined = false,
                    Name = "",
                    PetName = "",
                    Coin = 0,
                    Point = 0,
                    ActiveType = 0
                };
                return result;
            }
            else
            {
                string activeType;
                switch (model.ActiveType)
                {
                    case 1:
                        activeType = "扣点";
                        break;
                    case 2:
                        activeType = "包月";
                        break;
                    default:
                        activeType = "免费";
                        break;
                }
                var result = new
                {
                    Logined = true,
                    Name = model.UserName,
                    model.PetName,
                    model.Coin,
                    model.Point,
                    ActiveType = activeType
                };
                return result;
            }
        }

        #endregion

        #region 批量操作

        /// <summary>
        /// 批量操作
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="which"></param>
        public Result BatchToDo(string ids, string which)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ids) == false)
            {
                if (which != "0")
                {
                    Dal.BatchToDo(ids, which);
                }
                else
                {
                    res.Errors = "请选择要执行得操作。";
                }
            }
            else
            {
                res.Errors = "至少选择一个。";
            }
            return res;
        }

        #endregion

        #region 给会员管理界面绑定分页数据

        /// <summary>
        /// 给会员管理界面绑定分页数据
        /// </summary>
        /// <param name="isAdmin">是否管理员</param>
        /// <param name="sort">搜索字段</param>
        /// <param name="keyword">关键字</param>
        /// <param name="days">N天没有登录</param>
        /// <param name="pp">分页参数</param>
        /// <returns></returns>
        public IList<User> BindList(bool? isAdmin, string sort, string keyword, int? days, PagingParam pp)
        {
            DateTime? dDys;
            if (days == null)
            {
                dDys = null;
            }
            else
            {
                dDys = DateTime.Today.AddDays(-Convert.ToInt32(days) + 1).AddSeconds(-1);
            }

            return Dal.BindList(isAdmin, sort, keyword, dDys, pp);
        }

        #endregion
    }
}